using Microsoft.Extensions.DependencyInjection;
using ZeroBlog.Application.Commons.Dtos.In;
using ZeroBlog.Application.Commons.Dtos.Out;
using ZeroBlog.Application.Commons.Dtos.Query;
using ZeroBlog.Core.Domains.Blog;
using ZeroBlog.Core.DomainServices.Commons;
using ZeroSum.DependencyInjection.Attributes;
using ZeroSum.Extend.Mapper.Extensions;
using ZeroSum.Models;

namespace ZeroBlog.Application.Commons;

[Register(ServiceLifetime.Scoped)]
public class CommonApplicationService : ICommonApplicationService
{
    private readonly ICommonDomainService _domainService;

    public CommonApplicationService(ICommonDomainService domainService)
    {
        _domainService = domainService;
    }

    public async Task AddAsync(AddCommonDto dto)
    {
        var domain = dto.MapTo<Common>();
        await _domainService.AddAsync(domain);
    }

    public async Task DeleteAsync(DeleteCommonDto dto)
    {
        await _domainService.DeleteAsync(dto.Ids);
    }

    public async Task UpdateAsync(UpdateCommonDto dto)
    {
        var domain = dto.MapTo<Common>();
        await _domainService.UpdateAsync(domain);
    }

    public async Task<DetailCommonDto> Detail(DetailCommonQuery query)
    {
        var domain = await _domainService.GetAsync(x => x.Id == query.Id);
        return domain.MapTo<DetailCommonDto>();
    }

    public async Task<PagedResponseModel<PageCommonDto>> Page(PageCommonQuery query)
    {
        var (domains, count) = await _domainService.Query(query.Keyword, query.PageNumber, query.PageSize);
        var list = domains.MapTo<List<PageCommonDto>>();
        return new PagedResponseModel<PageCommonDto>(query.PageNumber, query.PageSize, count, list);
    }
}